//
//  YQRefundOrderDetailsVC.m
//  MuYing
//
//  Created by tbb on 2020/3/5.
//  Copyright © 2020 TBB. All rights reserved.
//

#import "YQRefundOrderDetailsVC.h"
#import "YQRefundOrderDetailsHeaderView.h"
#import "YQOrderDetailsBottonView.h"
#import "OrderEvaViewController.h"
#import "YQRefundDetailModel.h"
#import "YQOrderLogisticsWriteVC.h"
@interface YQRefundOrderDetailsVC ()<YQOrderDetailsBottonViewDelegate>
@property (nonatomic, strong) YQRefundOrderDetailsHeaderView * headerView;
@property (nonatomic, strong) YQOrderDetailsBottonView * bottonView;

@property (nonatomic, assign) NSInteger section;

@property (nonatomic, strong) YQRefundDetailModel * orderDetailsModel;
@property (nonatomic, copy) NSArray * orderDetailsTitleArr;
@end

@implementation YQRefundOrderDetailsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = kYQLS(@"订单详情");
    UIImage *rightImage = YQIMG(@"MY_nav_service_icon");
    self.rightItemImage = [rightImage qmui_imageWithTintColor:UIColor.whiteColor];
    self.section = 4;
    self.orderDetailsTitleArr = @[[NSString stringWithFormat:@"%@ :",kYQLS(@"订单编号")],[NSString stringWithFormat:@"%@ :",kYQLS(@"申请售后时间")]];
}

-(void)setupView {
    [super setupView];
    [self _setupNav];
    UIImage *leftImage = YQIMG(@"nav_back_sIcon");
    [self.leftItem setImage:[leftImage qmui_imageWithTintColor:UIColor.whiteColor] forState:(UIControlStateNormal)];
    [self.view addSubview:self.headerView];
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(154);
    }];
    self.listTableView.backgroundColor = HexColor(0xFAFAFA);
    self.listTableView.frame = CGRectMake(0, 154, kYQPScreenWidth, kYQPScreenHeight-Height_NavBar-kHeight_Botton_SafeArea - 154);

    kYQREGISTERTABLEVIEWNIBCELL(@"YQOrderManageGoodsCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"YQOrderDetailsMoneyCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"YQOrderDetailsLookLogisticsCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"YQOrderDetailsInfoCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"YQRefundOrderDetailsCell")
    
    kYQREGISTERTABLEVIEWCHeaderFooter([YQOrderDetailsTitleHeader class])
    self.listTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getRefundDetailsData];
    }];
    [self getRefundDetailsData];
}

-(void)rightItemAction:(UIButton *)sender {
    
}

#pragma mark - 获取售后订单详情接口
-(void)getRefundDetailsData {

    YQSHOWLOADINGINVIEW
    [YQPNetWorkingManager sendPostRequest:kPOST_REFUND_INFO_URL(self.orderModel.refundId) parameters:nil success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        [self stopRefresh];
        YQHIDELOADINGTIPS
        if (responseObject.code == 0) {
            
            self.orderDetailsModel = [YQRefundDetailModel modelWithJSON:responseObject.data];
            self.section = 4;
            self.headerView.refundModel = self.orderDetailsModel;
            [self.listTableView reloadData];
        }else{
            YQUITIPSERROR(responseObject.msg)
        }
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQHIDELOADINGTIPS
//        YQUITIPSERROR(error)
        [self stopRefresh];
    }];
    
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.section;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 1){
        return 4;
    }else if (section == 2){
        return 1;
    }else if (section == 0) {
        return 1;
    }
    else if (section == self.section - 1) {
        return 2;
    }
    return 1;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (indexPath.section == 0) {

        YQOrderManageGoodsCell *goodsCell = [tableView dequeueReusableCellWithIdentifier:@"YQOrderManageGoodsCell"];
        [goodsCell setOrderRefundDetailModel:self.orderDetailsModel];
        
        return goodsCell;
    }else if (indexPath.section == 1) {
        YQRefundOrderDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YQRefundOrderDetailsCell"];
        if (indexPath.row == 0) {
            cell.titleLb.text = kYQLS(@"退货理由");
            cell.subLb.text = self.orderDetailsModel.reason;;
        }else if (indexPath.row == 1){
            //售后类型，0是未收货仅退款，1是已收货（无需退货）仅退款，2用户退货退款 ,
            if(self.orderDetailsModel.status.intValue == 1){
                cell.subLb.text = kYQLS(@"已收货（无需退货）仅退款");
            }else{
                cell.subLb.text = kYQLS(@"用户退货退款");
            }
            cell.titleLb.text = kYQLS(@"货品状态");
        }else if (indexPath.row == 2){
            cell.titleLb.text = kYQLS(@"预计退款金额");
            cell.subLb.text = NSStringFormat(@"HKD%@",self.orderDetailsModel.amount);
            cell.subLb.textColor = kAPPThemeColor;
        }else{
            cell.subLb.text = kYQLS(@"退款金额将返还原账户");
            cell.titleLb.text = kYQLS(@"");
            cell.subLb.textColor = HexColor(0x999999);
        }
        return cell;
    
    }else if (indexPath.section == 2) {
        YQOrderDetailsMoneyCell *moneyCell = [tableView dequeueReusableCellWithIdentifier:@"YQOrderDetailsMoneyCell"];
        if (indexPath.row == 0) {
            moneyCell.labTitle.text = kYQLS(@"订单实付款");
            moneyCell.labSubTitle.text = [NSString stringWithFormat:@"HKD%@",self.orderDetailsModel.amount];
        }
        
        return moneyCell;
    
    }else if (indexPath.section == 3) {
        YQOrderDetailsInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YQOrderDetailsInfoCell"];
        cell.titleLb.text = self.orderDetailsTitleArr[indexPath.row];
        cell.infoLb.text = kYQLS(@"未创建");
        if (indexPath.row == 0) {
            cell.infoLb.text = self.orderDetailsModel.aftersaleSn;
        }else if (indexPath.row == 1) {
            cell.infoLb.text = self.orderDetailsModel.createTime;
        }

        return cell;
    }
    
    YQOrderDetailsLookLogisticsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YQOrderDetailsLookLogisticsCell"];
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 104;
    }else if (indexPath.section == 1){
        return 58;
        
    }else if (indexPath.section == 2) {
        return 30;
    }else if(indexPath.section == self.section -1) {
        return 21;
    }
    return 44;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//    if (section == self.section -1) {
//        YQOrderDetailsTitleHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"YQOrderDetailsTitleHeader"];
//        return header;
//    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    if(section == self.section -1) {
//        return 39;
//    }
    return 0.001;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end
#pragma mark --YQOrderDetailsBottonViewDelegate
- (void)selectItem:(NSInteger)itme {
    switch (self.orderDetailsModel.orderType.intValue) {
        case 1:
        {
            if (itme == 0) {
                //填写物流
                YQOrderLogisticsWriteVC *VC = [YQOrderLogisticsWriteVC new];
                VC.refundId = self.orderDetailsModel.refundId;
                kYQPUSH(VC);
            }else{
                //撤销申请
                [self refundCancelOrderModel:self.orderModel];
            }
        }
            break;
        case 2:
        {
            //撤销申请
            [self refundCancelOrderModel:self.orderModel];
        }
            break;
        case 4:
        {
            //重新申请
        }


        default:
            break;
    }
}

-(void)refundCancelOrderModel:(YQGoodsOrderModel *)orderModel {
    
    YQSHOWLOADINGINVIEW
    [YQPNetWorkingManager sendGetRequest:kGET_REFUND_CANCEL_URL(orderModel.orderId) parameters:nil success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        YQUITIPSSUCCEED(responseObject.msg)
        if (responseObject.code == 0) {
            [self getRefundDetailsData];
        }
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQUITIPSERROR(error)
        YQHIDELOADINGTIPS
    }];
}


#pragma mark --YQOrderDetailsBottonViewDelegate end
#pragma -mark 私有
-(void)_setupNav {
    UIFont *font = kSystemMedium(17);
    NSDictionary *dic = @{ NSFontAttributeName:font,NSForegroundColorAttributeName:UIColor.whiteColor};
    self.navigationController.navigationBar.titleTextAttributes =dic;
    // 一行代码搞定导航栏颜色
    [self wr_setNavBarBarTintColor:kAPPThemeColor];
    // 一行代码搞定导航栏底部分割线是否隐藏
    [self wr_setNavBarShadowImageHidden:YES];
    [self wr_setNavBarTitleColor:[UIColor whiteColor]];
    [self wr_setNavBarTintColor:UIColor.whiteColor];
}


#pragma makr -- lazy
-(YQRefundOrderDetailsHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [YQRefundOrderDetailsHeaderView viewFromXib];
        _headerView.topBgView.backgroundColor = kAPPThemeColor;
        _headerView.stateLb.text = kYQLS(@"确认收货");
        _headerView.timeLb.text = @"剩余: 05小时57小时";
        _headerView.nameLb.text = @"李晓冬";
        _headerView.phoneLb.text = @"17727956732";
        _headerView.locationLb.text = @"地址 : 广东省深圳市南山区蛇口街道创业一号C栋";
    }
    return _headerView;
}

-(YQOrderDetailsBottonView *)bottonView {
    if (!_bottonView) {
        _bottonView = [YQOrderDetailsBottonView viewFromXib];
        _bottonView.backgroundColor = UIColor.whiteColor;
        //2.设置阴影偏移范围
        _bottonView.layer.shadowOffset = CGSizeMake(0, 2);
        //3.设置阴影颜色的透明度
        _bottonView.layer.shadowOpacity = 0.5;
        
        //4.设置阴影半径
        _bottonView.layer.shadowRadius = 4;
        //5.设置阴影路径
        _bottonView.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.navigationController.navigationBar.bounds].CGPath;
        _bottonView.delegate = self;
        _bottonView.leftBtn.hidden = YES;

    }
    return _bottonView;
}

#pragma makr -- lazy end
@end
